﻿using System;
using System.Collections.Generic;
using System.IO;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using FlexCell;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKCommonTableTool;
using YJKDrawSelectArea;
using YJKStatisticTable.FastStatisticTable.Utility;

namespace YJKStatisticTable.DoorAndWindowTable
{
	[Transaction(TransactionMode.Manual)]
	internal class CmdRefreshTable : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			this.m_cmmdData = commandData;
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			this.m_doc = activeUIDocument.Document;
			FilterGroup filterGroup = new FilterGroup();
			Reference reference = null;
			try
			{
				reference = activeUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, filterGroup, "请选择一个表格");
			}
			catch
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			Group group = this.m_doc.GetElement(reference) as Group;
			if (group != null)
			{
				List<short> list = new List<short>();
				List<short> list2 = new List<short>();
				List<CellExtendedData> list3 = new List<CellExtendedData>();
				XYZ ptPos = new XYZ();
				GridExtendedData.ReadGridExtendedData(ref ptPos, group, ref list, ref list2, ref list3, ref this.m_dataRefresh);
				if (this.m_dataRefresh.DocName != this.m_doc.Title)
				{
					YJKMessageBox.Show("表格对应文档和当前文档不一致，不能进行刷新");
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				FlexCell.Grid grid = new FlexCell.Grid();
				if (!this.InitialGridData(ref grid))
				{
					YJKMessageBox.Show("初始化表格失败");
					return Autodesk.Revit.UI.Result.Failed;
				}
				GridToolExportImportRevit gridToolExportImportRevit = new GridToolExportImportRevit(commandData, ptPos);
				gridToolExportImportRevit.SetRefreshData(this.m_dataRefresh);
				Transaction transaction = new Transaction(this.m_doc, "DeleteOldGrid");
				transaction.Start();
				this.m_doc.Delete(group.Id);
				transaction.Commit();
				if (gridToolExportImportRevit.SetGrid(commandData, grid, CREATENEWVIEW.NONE))
				{
					gridToolExportImportRevit.DrawInRevitView();
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private bool InitialGridData(ref FlexCell.Grid grid)
		{
			string text = Path.Combine(Product.InstallLocation, "Templates", "StatisticTempletlFile\\");
			text = text + this.m_dataRefresh.GridSetInfo.TemplateName + ".hymb";
			if (!grid.LoadFromXML(text))
			{
				return false;
			}
			if (this.m_dataRefresh.GridSetInfo.TemplateName == "门窗表1")
			{
				DoorWindowGrid1 doorWindowGrid = new DoorWindowGrid1(this.m_doc, this.m_dataRefresh.GridSetInfo, ref grid);
				if (this.m_dataRefresh.StatisticType == 2)
				{
					List<Element> elementsInSelArea = this.GetElementsInSelArea();
					return doorWindowGrid.GetStatisticGrid(elementsInSelArea);
				}
				List<ViewPlan> viewPlan = this.GetViewPlan();
				return doorWindowGrid.GetStatisticGrid(viewPlan);
			}
			else if (this.m_dataRefresh.GridSetInfo.TemplateName == "门窗表2")
			{
				DoorWindowGrid2 doorWindowGrid2 = new DoorWindowGrid2(this.m_doc, this.m_dataRefresh.GridSetInfo, ref grid);
				if (this.m_dataRefresh.StatisticType == 0)
				{
					List<ViewPlan> viewPlan2 = this.GetViewPlan();
					return doorWindowGrid2.GetStatisticGrid(viewPlan2);
				}
				if (this.m_dataRefresh.StatisticType == 1)
				{
					List<ViewPlan> viewPlan3 = this.GetViewPlan();
					return doorWindowGrid2.GetStatisticGrid(viewPlan3, this.m_dataRefresh.StandardFloor);
				}
				List<Element> elementsInSelArea2 = this.GetElementsInSelArea();
				return doorWindowGrid2.GetStatisticGrid(elementsInSelArea2);
			}
			else
			{
				if (!(this.m_dataRefresh.GridSetInfo.TemplateName == "门窗表3"))
				{
					return false;
				}
				DoorWindowGrid3 doorWindowGrid3 = new DoorWindowGrid3(this.m_doc, this.m_dataRefresh.GridSetInfo, ref grid);
				if (this.m_dataRefresh.StatisticType == 0)
				{
					List<ViewPlan> viewPlan4 = this.GetViewPlan();
					return doorWindowGrid3.GetStatisticGrid(viewPlan4);
				}
				if (this.m_dataRefresh.StatisticType == 1)
				{
					List<ViewPlan> viewPlan5 = this.GetViewPlan();
					return doorWindowGrid3.GetStatisticGrid(viewPlan5, this.m_dataRefresh.StandardFloor);
				}
				List<Element> elementsInSelArea3 = this.GetElementsInSelArea();
				return doorWindowGrid3.GetStatisticGrid(elementsInSelArea3);
			}
		}

		private List<Element> GetElementsInSelArea()
		{
			List<Element> result = new List<Element>();
			List<ElementId> selAreaCurveId = this.GetSelAreaCurveId();
			if (selAreaCurveId.Count <= 0)
			{
				return result;
			}
			ViewPlan view = this.m_doc.GetElement(this.m_dataRefresh.SelCurViewPlan) as ViewPlan;
			CollectSelAreaElement collectSelAreaElement = new CollectSelAreaElement(this.m_cmmdData.Application);
			result = collectSelAreaElement.CollectAllElementInArea(this.m_dataRefresh.GridSetInfo.LinkFile, view, selAreaCurveId);
			this.DeleteSelectLine(selAreaCurveId);
			return result;
		}

		private List<ElementId> GetSelAreaCurveId()
		{
			ViewPlan viewPlan = this.m_doc.GetElement(this.m_dataRefresh.SelCurViewPlan) as ViewPlan;
			List<ElementId> list = new List<ElementId>();
			Transaction transaction = new Transaction(this.m_doc, "Create Select Curve");
			transaction.Start();
			try
			{
				foreach (SelectAreaCurve selectAreaCurve in this.m_dataRefresh.SelAreaCurve)
				{
					List<XYZ> points = selectAreaCurve.Points;
					if (points.Count == 2)
					{
						Line line = Line.CreateBound(points[0], points[1]);
						DetailCurve detailCurve = this.m_doc.Create.NewDetailCurve(viewPlan, line);
						list.Add(detailCurve.Id);
					}
					else if (points.Count == 3)
					{
						Arc arc = Arc.Create(points[0], points[1], points[2]);
						DetailCurve detailCurve2 = this.m_doc.Create.NewDetailCurve(viewPlan, arc);
						list.Add(detailCurve2.Id);
					}
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
			return list;
		}

		private void DeleteSelectLine(List<ElementId> lstSelLineId)
		{
			Transaction transaction = new Transaction(this.m_doc, "Delete SelArea Line");
			transaction.Start();
			try
			{
				this.m_doc.Delete(lstSelLineId);
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		private List<ViewPlan> GetViewPlan()
		{
			List<ViewPlan> list = new List<ViewPlan>();
			foreach (ElementId elementId in this.m_dataRefresh.ViewPlans)
			{
				ViewPlan viewPlan = this.m_doc.GetElement(elementId) as ViewPlan;
				if (viewPlan != null)
				{
					list.Add(viewPlan);
				}
			}
			return list;
		}

		private ExternalCommandData m_cmmdData;

		private Document m_doc;

		private GridDataRefresh m_dataRefresh = new GridDataRefresh();
	}
}
